Alteryx: How to Create Folders Dynamically

Alteryx: How to Create Folders Dynamically

Clock Icon2021.02.18

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

(日本語はこちらから)

Background

In Alteryx it’s possible to find a dynamic solution for your workflow. In this post, you can get to know how to generate a dynamic folder depending on your requirements such as data fields, dates etc. You can then choose to output the results to such dynamic folders. This will help to better organize your results.

How To Achieve

1) Using “mkdir” command:

In order to create a dynamic folder, you have to set some criteria for naming the new folders. The folder name can be taken from an existing field such as Region, Manager, id, Store, Filetype, etc. or it can be taken from a variable field such as File path, System date, Timestamp etc. To demonstrate, we will use Date as the input criteria for the folder name.

Let us assume that a field has date values as shown below, and you wanted to create a folder each for the date listed here.

Add a formula tool to create a windows command using the operator “mkdir” as shown below:

In the next step, using the select tool remove all the data fields except the “Command” field created above. Finally add the “Run Command” tool from the “Developer” category.

Using this Run Command tool, Alteryx can run external programs from the workflow. Choose to output a batch file, and make sure it’s “File Format” is saved as CSV. Now allow Alteryx to run this external batch file by running the workflow.

If any of the target folder already exists in the directory then it will be skipped (not replaced) but if the target folder is absent then it will be newly created.

* Note: In the above method, if the target field (data in the column used for creating folder names) contains “space” then the folder name will be truncated only upto the “space”, this is because windows command “mkdir” cannot recognize any text after the “space”. Similarly, if there were some special characters then those may get eliminated in the output folder name. So if your data contain spacers then it is better to use a different approach as described below.

2) Using Batch Macro:

This method is especially helpful when the data contains space. It can create folders with folder-name that contain spaces, and it also allows special characters such as:

However, please note that, due to Windows limitations some special characters are not permitted for creating folder names. These are shown below:

With this in mind, let us create a batch macro. Let us assume that our data is represented as shown here:

Our goal here is to create new folders based on this column (one folder per entry), which means our folder names will contain alphanumeric special characters separated by spacers.

We begin by adding a formula tool to create the full path for the final destination.

Next, we choose to select only this field which is represented by the full path. A batch macro will be created with the following instructions.

In the batch macro create a placeholder for the full path by adding a “Text Input” tool and an arbitrary input value, now add the “Run Command”. The settings for the run command are shown below. The output file is used to store temporary data, cmd.exe is and external program called by Alteryx, the command argument is the input received from the processing data. Additionally, place a “Control Parameter” in this macro and name it to match the input data’s column name.

In the Workflow Configuration window, select the Workflow tab and change some settings. For the Constants Type, set the Question value as the destination directory (in this case c:\temp)

Save this macro and go back to the initial workflow, add the macro and select the input field as “Folder” as shown below.

The output of this workflow is displayed here:

* Note: Whenever this workflow runs, new folders will be created if they are previously absent. If some of those folders are already pre-existing in the said directory, then those folders are skipped and remaining folders will be created, however if all the folders are already existing then no folders are created and an error message will be displayed for process failure.

Summary

Dynamically creating folders in Alteryx is very helpful, especially when a lot of folders are needed based on specific rules. In this post, two methods are described for creating dynamic folders using Alteryx.

Alteryxの導入なら、クラスメソッドにおまかせください

日本初のAlteryxビジネスパートナーであるクラスメソッドが、Alteryxの導入から活用方法までサポートします。14日間の無料トライアルも実施中ですので、お気軽にご相談ください。

alteryx_960x400

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.